package cz.fel.via.timemanagement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cz.cvut.fel.via.kosapirest.Configuration;
import cz.cvut.fel.via.kosapirest.Credentials;
import cz.cvut.fel.via.kosapirest.Fasade;
import cz.cvut.fel.via.kosapirest.domain.Note;
import cz.cvut.fel.via.kosapirest.impl.FasadeImpl;

//kontext aplikace
public class TimeManagement {

	private static boolean isFel = true;
	//private static Fasade fasade = null;
	//private static Credentials credentials = null;
	
	static final String FEL_SITE = "https://kosapi.feld.cvut.cz/api/3b/";
	static final String FIT_SITE = "https://kosapi.fit.cvut.cz/api/3b/";
	static final String dataserverUrl = "http://dataserver-prochka6.rhcloud.com/api";
	
	private static Credentials credentials;
	private static Fasade fasade;
	/**
	 * Uchovava data potrebna pro prenos mezi aktivitami.
	 */
	private final static Map<String, Object> data = new HashMap<String, Object>();
	
	static Fasade getFasade() {
		if (credentials == null) {
			return null;
		}
		
		Configuration config = new Configuration()
		.set(Configuration.DATASERVER_URI, dataserverUrl)
		.set(Configuration.KOS_API_CREDENTIALS,credentials);
		
		
		if (fasade == null) {
			if (isFel) {
				config.set(Configuration.KOS_API_URI,FEL_SITE);
				fasade = new FasadeImpl(config);
			} else {
				config.set(Configuration.KOS_API_URI,FIT_SITE);
				fasade = new FasadeImpl(config);
			}
		}
		return fasade;
	}
	
	static void setCredentials(String login, String pwd) {
		credentials = new Credentials(login, pwd);
	}
	
	static void deleteFasade() {
		fasade = null;
		credentials = null;
	}
	
	static void setFel() {
		isFel = true;
	}
	
	static void setFit() {
		isFel = false;
	}
	
	static List<Note> findNotes() {
		return fasade.findNotes(credentials.getUsername());
	}
	
	static String getUser() {
		if (credentials == null) return "";
		return credentials.getUsername();
	}
	
	/**
	 * Vlozi data do pameti aplikace
	 * @param key Klic, pod kterym budou data pristupna
	 * @param value Objekt, ktery uchovavame
	 */
	public static void putKey(String key, Object value) {
		data.put(key, value);
	}
	
	/**
	 * Vybere data z pameti aplikace
	 * @param key Klic, pod kterym jsou data ulozena
	 * @return Objekt, tkery byl uschovan
	 */
	public static Object getKey(String key) {
		Object o = data.get(key);
		if (o != null) {
			data.remove(key);
		}
		return o;
	}

}
